package com.example.spindrift.service.Util;

import com.example.spindrift.service.Model.Mp3Message;
import com.mpatric.mp3agic.ID3v2;
import com.mpatric.mp3agic.InvalidDataException;
import com.mpatric.mp3agic.Mp3File;
import com.mpatric.mp3agic.UnsupportedTagException;
import org.jaudiotagger.audio.exceptions.InvalidAudioFrameException;
import org.jaudiotagger.audio.exceptions.ReadOnlyFileException;
import org.jaudiotagger.audio.mp3.MP3File;
import org.jaudiotagger.tag.TagException;
import org.jaudiotagger.tag.id3.AbstractID3v2Tag;

import java.io.File;
import java.io.IOException;


public class MP3Analysis {
    /**
     * 从MP3File对象中获取 ID3v2 标签信息
     * @param file MP3File对象
     * @return 返回MP3Message ID3v2信息模型对象
     */
    public static Mp3Message getMP3Message(File file) throws ReadOnlyFileException, TagException, InvalidAudioFrameException, IOException {
        MP3File musicFile = new MP3File(file);
        Mp3Message message = new Mp3Message();//实例化MP3信息模型对象
        AbstractID3v2Tag tag = musicFile.getID3v2Tag();

        message.setSongName(tag.getFirst("TIT2"));
        message.setSingerName(tag.getFirst("TPE1"));
        message.setAlbum(tag.getFirst("TALB"));
        message.setSongType(tag.getFirst("TCON"));
        message.setAlbumImage(tag.getFirstArtwork().getBinaryData());
        return message;
    }
    public static Mp3Mate getMP3Mate(File file) throws InvalidDataException, IOException, UnsupportedTagException {
        Mp3Mate mate = null;
        Mp3File mp3file = new Mp3File(file);
        if (mp3file.hasId3v2Tag()) {
            ID3v2 id3v2Tag = mp3file.getId3v2Tag();
            mate = new Mp3Mate(
                    id3v2Tag.getTrack(),
                    id3v2Tag.getArtist(),
                    id3v2Tag.getTitle(),
                    id3v2Tag.getAlbum(),
                    mp3file.getLengthInSeconds(),
                    id3v2Tag.getAlbumImage(),
                    id3v2Tag.getAlbumImageMimeType(),
                    id3v2Tag.getYear(),
                    id3v2Tag.getGenre(),
                    id3v2Tag.getLyrics(),
                    id3v2Tag.getComposer(),
                    id3v2Tag.getPublisher(),
                    id3v2Tag.getEncoder()
            );
//            System.out.println("唱片歌曲数量: " + id3v2Tag.getTrack());
//            System.out.println("艺术家: " + id3v2Tag.getArtist());
//            System.out.println("歌曲名: " + id3v2Tag.getTitle());
//            System.out.println("唱片名: " + id3v2Tag.getAlbum());
//            System.out.println("歌曲长度:"+mp3file.getLengthInSeconds()+"秒");
//            System.out.println("码率: " + mp3file.getBitrate() + " kbps " + (mp3file.isVbr() ? "(VBR)" : "(CBR)"));
//            System.out.println("专辑插画 : "+id3v2Tag.getAlbumImage());
//            System.out.println("专辑插画类型"+id3v2Tag.getAlbumImageMimeType());
//            System.out.println("发行时间: " + id3v2Tag.getYear());
//            System.out.println("流派: " + id3v2Tag.getGenre() + " (" + id3v2Tag.getGenreDescription() + ")");
//            System.out.println("注释: " + id3v2Tag.getComment());
//            System.out.println("歌词: " + id3v2Tag.getLyrics());
//            System.out.println("作曲家: " + id3v2Tag.getComposer());
//            System.out.println("发行公司: " + id3v2Tag.getPublisher());
//            System.out.println("Original artist: " + id3v2Tag.getOriginalArtist());
//            System.out.println("Album artist: " + id3v2Tag.getAlbumArtist());
//            System.out.println("版权: " + id3v2Tag.getCopyright());
//            System.out.println("URL: " + id3v2Tag.getUrl());
//            System.out.println("编码格式: " + id3v2Tag.getEncoder());
//            byte[] albumImageData = id3v2Tag.getAlbumImage();
//            if (albumImageData != null) {
//                System.out.println("专辑插图长度: " + albumImageData.length + " bytes");
//                System.out.println("专辑插图类型: " + id3v2Tag.getAlbumImageMimeType());
//            }
        }
        return mate;
    }
    public static class Mp3Mate{
        /**
         * 唱片歌曲数量
         */
        private String track;
        /**
         * 艺术家
         */
        private String artist;
        /**
         * 歌曲名
         */
        private String title;
        /**
         * 唱片名
         */
        private String album;
        /**
         * 歌曲长度
         */
        private Long lengthInSeconds;
        /**
         * 专辑插画
         */
        private byte[] albumImage;
        /**
         * 专辑插画类型
         */
        private String albumImageMimeType;
        /**
         * 发布年份
         */
        private String year;
        /**
         * 流派
         */
        private Integer genre;
        /**
         * 歌词
         */
        private String lyrics;
        /**
         * 作曲家
         */
        private String composer;
        /**
         * 发行公司
         */
        private String publisher;
        /**
         * 编码格式
         */
        private String encoder;

        public Mp3Mate(String track, String artist, String title, String album, Long lengthInSeconds, byte[] albumImage, String albumImageMimeType, String year, Integer genre, String lyrics, String composer, String publisher, String encoder) {
            this.track = track;
            this.artist = artist;
            this.title = title;
            this.album = album;
            this.lengthInSeconds = lengthInSeconds;
            this.albumImage = albumImage;
            this.albumImageMimeType = albumImageMimeType;
            this.year = year;
            this.genre = genre;
            this.lyrics = lyrics;
            this.composer = composer;
            this.publisher = publisher;
            this.encoder = encoder;
        }

        public String getTrack() {
            return track;
        }

        public String getArtist() {
            return artist;
        }

        public String getTitle() {
            return title;
        }

        public String getAlbum() {
            return album;
        }

        public Long getLengthInSeconds() {
            return lengthInSeconds;
        }

        public byte[] getAlbumImage() {
            return albumImage;
        }

        public String getAlbumImageMimeType() {
            return albumImageMimeType;
        }

        public String getYear() {
            return year;
        }

        public Integer getGenre() {
            return genre;
        }

        public String getLyrics() {
            return lyrics;
        }

        public String getComposer() {
            return composer;
        }

        public String getPublisher() {
            return publisher;
        }

        public String getEncoder() {
            return encoder;
        }
    }
}
